﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LdCms.Plugins.Sms.Tencent
{
    using LdCms.Common.Extension;
    using LdCms.Common.Json;
    using LdCms.Common.Logs;
    using LdCms.Common.Net;
    using LdCms.Common.Security;
    using LdCms.Common.Web;

    public class TencentSmsCallbackHelper
    {
        private string strRand = GeneralCodeHelper.GetRandomInt(8);
        public string GetCallbackData()
        {
            try
            {
                return HttpHelper.RequestInputStream();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public string GetSmsCallback()
        {
            try
            {
                string uri = HttpContext.Current.Request.GetAbsoluteUri();
                string method = HttpContext.Current.Request.GetHttpMethod();
                string contentType = HttpContext.Current.Request.GetHttpContentType();
                string result = string.Format("{{\"data\":{0}}}", GetCallbackData());
                LogsManager.LogPath = string.Empty;
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-腾讯短信下发状态通知--开结--", strRand));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-IP：{1}", strRand, Net.Ip));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-地址：{1}", strRand, uri));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-动作：{1}", strRand, method));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-类型：{1}", strRand, contentType));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-结果：{1}", strRand, result.MiniClear()));
                LogsManager.WriteLog(LogsFile.Debug, string.Format("[{0}]-腾讯短信下发状态通知--结束--", strRand));
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SmsStatusCallback GetSmsStatusCallbackPro()
        {
            try
            {
                string result = GetSmsCallback();
                return result.ToObject<SmsStatusCallback>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SmsReplyCallback GetSmsReplyCallbackPro()
        {
            try
            {
                string result = GetSmsCallback();
                return result.ToObject<SmsReplyCallback>();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public string ResponseResult(int result)
        {
            try
            {
                SmsCallbackResult entity = new SmsCallbackResult()
                {
                    result = result,
                    errmsg = result == 0 ? "ok" : "error"
                };
                return entity.ToJson();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
